<?php
//--------------include framework_config file--------------------------------------------------------------
include("application/app_config.php");
include("application/app_constant.php");
include ("library/config/fw_config.php");
$smarty->template_dir = TEMPLATE_DIR;
$smarty->compile_dir  = TEMPLATE_C_DIR;
$smarty->config_dir  = TEMPLATE_DIR;
//////////////////////////////////////////////////////////////////
// Custom error handler to log PEAR errors
if (!defined('PEAR_ERROR_LOG_FILE')) {
	define('PEAR_ERROR_LOG_FILE', APPLICATION_DIR.'/errors.log');
}
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'customErrorHandler');
function customErrorHandler($error) {
	if (is_a($error,'PEAR_Error')) {
		$message = $error->getUserInfo();
	} else {
		$message = $error;
	}
	$log = '';
	$log .= "-------------------\n";
	$log .= 'Request : '.$_SERVER['REQUEST_URI'].'	 Time : '.date('Y-m-d H:i:s')."\n";
	$log .= "-------------------\n";
	$log .= $message."\n";
	file_put_contents(PEAR_ERROR_LOG_FILE, $log, FILE_APPEND);
}

//-------------------------route controller----------------------------------------------------------------
$controller = isset($_REQUEST["m"])? strtolower($_REQUEST["m"])	: "";
$action	 = isset($_REQUEST["a"])? strtolower($_REQUEST["a"])	: "";

//register var modules, action and args
DRX::register("controller",$controller);
DRX::register("action",$action);

include_once "config.php";
include_once "autorisation.php";

//--------------include language file----------------------------------------------------------------------
if (DRX_MultiLanguage::getLanguage() != '')
{
	include_once (APPLICATION_DIR."/application/language/".DRX_MultiLanguage::getLanguage()."/common.php");
}

// check whether or not user has already login
if ((isset($_SESSION["is_admin"]) && $_SESSION["is_admin"] > 0)
		|| (isset($_SESSION["is_agent"]) && $_SESSION["is_agent"] > 0)
		|| (isset($_SESSION["is_annonceur"]) && $_SESSION["is_annonceur"] > 0))
{
	// TRANSFORM ACTION, 'transform_act' is flag for TRANFORM ACTION
	// value 1 is valid, value != 1 is invalid
    
	if (Request::getIntNumber('act') != ACT_TRANSFORM && Request::getIntNumber('act') != ACT_RETRANSFORM)
	{
		// if request pages are: login and lost_password,
		// forward to default page, belong to user type
		if (($controller == "login" && $action == "login")
			|| ($controller == "login" && $action == "lost_password"))
		{
				redirect($_SESSION["url_demande"]);
		}
	}
}

if ($action == "" && file_exists("application/controller/".$controller.".php"))
{

	require_once("application/controller/".$controller.".php");
}
elseif(file_exists("application/controller/".$controller."/".$action.".php"))
{
	require_once("application/controller/".$controller."/".$action.".php");
}
else
{
	redirect(APPLICATION_URL . "/home/list");
}
